//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::google::protobuf::Any;



pub mod board {
    use prost::Message;
    use proto_gen_rust::TypeUrl;
    use proto_gen_rust::google::protobuf::Any;
    use proto_gen_rust::notices_board;

    #[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
    pub enum Notice {
        CreateBoardNotice(notices_board::CreateBoardNotice),
        UpdateBoardNotice(notices_board::UpdateBoardNotice),
        RemoveBoardNotice(notices_board::RemoveBoardNotice),
        JoinBoardNotice(notices_board::JoinBoardNotice),
        LeaveBoardNotice(notices_board::LeaveBoardNotice),
        UpdateBoardMemberPermNotice(notices_board::UpdateBoardMemberPermNotice),
        CreateDrawNotice(notices_board::CreateDrawNotice),
        UpdateDrawNotice(notices_board::UpdateDrawNotice),
        RemoveDrawNotice(notices_board::RemoveDrawNotice),
        CreateNodeNotice(notices_board::CreateNodeNotice),
        UpdateNodeNotice(notices_board::UpdateNodeNotice),
        RemoveNodeNotice(notices_board::RemoveNodeNotice),
        CreateEdgeNotice(notices_board::CreateEdgeNotice),
        UpdateEdgeNotice(notices_board::UpdateEdgeNotice),
        RemoveEdgeNotice(notices_board::RemoveEdgeNotice),
        RemoveEdgeByHandleNotice(notices_board::RemoveEdgeByHandleNotice),
        UserMouseChangeNotice(notices_board::UserMouseChangeNotice),
        ExtDataConfigChangeNotice(notices_board::ExtDataConfigChangeNotice),
        NewChatMsgNotice(notices_board::NewChatMsgNotice),
    }

    pub fn decode_notice(data: &Any) -> Option<Notice> {
        if data.type_url == notices_board::CreateBoardNotice::type_url() {
            if let Ok(notice) = notices_board::CreateBoardNotice::decode(data.value.as_slice()) {
                return Some(Notice::CreateBoardNotice(notice));
            }
        } else if data.type_url == notices_board::UpdateBoardNotice::type_url() {
            if let Ok(notice) = notices_board::UpdateBoardNotice::decode(data.value.as_slice()) {
                return Some(Notice::UpdateBoardNotice(notice));
            }
        } else if data.type_url == notices_board::RemoveBoardNotice::type_url() {
            if let Ok(notice) = notices_board::RemoveBoardNotice::decode(data.value.as_slice()) {
                return Some(Notice::RemoveBoardNotice(notice));
            }
        } else if data.type_url == notices_board::JoinBoardNotice::type_url() {
            if let Ok(notice) = notices_board::JoinBoardNotice::decode(data.value.as_slice()) {
                return Some(Notice::JoinBoardNotice(notice));
            }
        } else if data.type_url == notices_board::LeaveBoardNotice::type_url() {
            if let Ok(notice) = notices_board::LeaveBoardNotice::decode(data.value.as_slice()) {
                return Some(Notice::LeaveBoardNotice(notice));
            }
        } else if data.type_url == notices_board::UpdateBoardMemberPermNotice::type_url() {
            if let Ok(notice) =
                notices_board::UpdateBoardMemberPermNotice::decode(data.value.as_slice())
            {
                return Some(Notice::UpdateBoardMemberPermNotice(notice));
            }
        } else if data.type_url == notices_board::CreateDrawNotice::type_url() {
            if let Ok(notice) = notices_board::CreateDrawNotice::decode(data.value.as_slice()) {
                return Some(Notice::CreateDrawNotice(notice));
            }
        } else if data.type_url == notices_board::UpdateDrawNotice::type_url() {
            if let Ok(notice) = notices_board::UpdateDrawNotice::decode(data.value.as_slice()) {
                return Some(Notice::UpdateDrawNotice(notice));
            }
        } else if data.type_url == notices_board::RemoveDrawNotice::type_url() {
            if let Ok(notice) = notices_board::RemoveDrawNotice::decode(data.value.as_slice()) {
                return Some(Notice::RemoveDrawNotice(notice));
            }
        } else if data.type_url == notices_board::CreateNodeNotice::type_url() {
            if let Ok(notice) = notices_board::CreateNodeNotice::decode(data.value.as_slice()) {
                return Some(Notice::CreateNodeNotice(notice));
            }
        } else if data.type_url == notices_board::UpdateNodeNotice::type_url() {
            if let Ok(notice) = notices_board::UpdateNodeNotice::decode(data.value.as_slice()) {
                return Some(Notice::UpdateNodeNotice(notice));
            }
        } else if data.type_url == notices_board::RemoveNodeNotice::type_url() {
            if let Ok(notice) = notices_board::RemoveNodeNotice::decode(data.value.as_slice()) {
                return Some(Notice::RemoveNodeNotice(notice));
            }
        } else if data.type_url == notices_board::CreateEdgeNotice::type_url() {
            if let Ok(notice) = notices_board::CreateEdgeNotice::decode(data.value.as_slice()) {
                return Some(Notice::CreateEdgeNotice(notice));
            }
        } else if data.type_url == notices_board::UpdateEdgeNotice::type_url() {
            if let Ok(notice) = notices_board::UpdateEdgeNotice::decode(data.value.as_slice()) {
                return Some(Notice::UpdateEdgeNotice(notice));
            }
        } else if data.type_url == notices_board::RemoveEdgeNotice::type_url() {
            if let Ok(notice) = notices_board::RemoveEdgeNotice::decode(data.value.as_slice()) {
                return Some(Notice::RemoveEdgeNotice(notice));
            }
        } else if data.type_url == notices_board::RemoveEdgeByHandleNotice::type_url() {
            if let Ok(notice) =
                notices_board::RemoveEdgeByHandleNotice::decode(data.value.as_slice())
            {
                return Some(Notice::RemoveEdgeByHandleNotice(notice));
            }
        } else if data.type_url == notices_board::UserMouseChangeNotice::type_url() {
            if let Ok(notice) = notices_board::UserMouseChangeNotice::decode(data.value.as_slice())
            {
                return Some(Notice::UserMouseChangeNotice(notice));
            }
        } else if data.type_url == notices_board::ExtDataConfigChangeNotice::type_url() {
            if let Ok(notice) =
                notices_board::ExtDataConfigChangeNotice::decode(data.value.as_slice())
            {
                return Some(Notice::ExtDataConfigChangeNotice(notice));
            }
        } else if data.type_url == notices_board::NewChatMsgNotice::type_url() {
            if let Ok(notice) = notices_board::NewChatMsgNotice::decode(data.value.as_slice()) {
                return Some(Notice::NewChatMsgNotice(notice));
            }
        }
        None
    }
}

pub mod client {
    #[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
    #[serde(rename_all = "snake_case")]
    pub struct WrongSessionNotice {
        pub name: String, //用于区分发生错误的地方
    }

    #[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
    #[serde(rename_all = "snake_case")]
    pub struct AdminLoginNotice {}

    #[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
    pub enum Notice {
        WrongSessionNotice(WrongSessionNotice),
        AdminLoginNotice(AdminLoginNotice),
    }
}

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
pub enum NoticeMessage {
    BoardNotice(board::Notice),
    ClientNotice(client::Notice),
}

pub fn decode_notice(data: &Any) -> Option<NoticeMessage> {
    if let Some(ret) = board::decode_notice(data) {
        return Some(NoticeMessage::BoardNotice(ret));
    }
    None
}

pub fn new_wrong_session_notice(name: String) -> NoticeMessage {
    return NoticeMessage::ClientNotice(client::Notice::WrongSessionNotice(
        client::WrongSessionNotice { name: name },
    ));
}

pub fn new_admin_login_notice() -> NoticeMessage {
    return NoticeMessage::ClientNotice(client::Notice::AdminLoginNotice(
        client::AdminLoginNotice {},
    ));
}
